#ifndef BATTERY_H
#define BATTERY_H

//TODO: En  la documentacion, la funcion de battery_init poner el link de battery.c, y BATT_PIN. tambien poner documentacion externa para las variables
// declaradas en el Makefile, como ON y OFF.

/**
 * @defgroup nxbot_batt BATTERY Library
 * @ingroup nxbot_low_level
 * @code #include <battery.h> @endcode
 * @brief Interrupt Battery library for checking the state of the %battery in NXBOT. 
 *
 * This module uses the built-in ADC of the ATmega1281 microcontroller. The %battery is connected to a voltage divider, in order to obtain 
 * permisible voltage values for the microcontroller pins. Thus, when using a 6V full-charged %battery, the voltage divider equals 1/2. Using a 
 * 12V full-charged %battery, the voltage divider equals 1/4. The pin used by the microcontroller is the BATT_PIN, defined in %battery.c.
 * The ADC is used at <b>8 bit-resolution</b>, and the readings are <b>interrupt-driven</b>. 
 * That means, the module updates constantly the %battery state variable,
 * only if the interrupts are active. Therefore, it is necessary to enable interrupts, for example via the <i>sei()</i> function.
 */


/**@{*/

/**
 * @brief constant definition for battery_read
 * @see battery_read
 *
 */
#define BATT_READ_VOLT 0
/**
 * @brief constant definition for battery_read
 * @see battery_read
 *
 */
#define BATT_READ_PERC 1

/**
 * @brief Hardware initialization for the %battery module.
 *
 * Configures the ADC, enabling channel ADC0. VREF is the supply voltage (expected 5V).
 */
void battery_init(void);
/**
 * @brief Function used to write the state of the %battery-LED, located in the rear of NXBot.
 *
 * CAUTION!: This function is only used for debugging purposes. The LED is automatically activated, if the %battery is full-charged. 
 * If the %battery charge is below some predefined limit (5,2V), the LED will be automatically deactivated.
 *
 * @param en 1 for enabling the LED, 0 for disabling. Instead of these values, use either ON or OFF constants, defined globally in Makefile.
 */
void battery_ledEnable(unsigned char en);
/**
 * @brief Reads the state of the %battery, in [%] units.
 *
 * This function returns the last calculated value for the %battery state. 
 * The calculation of the state value is done in the ISR, each time a reading is complete. 
 * That means, when the interrupts are not enabled, the reading will not be
 * actual neither accurate.
 *
 * @return The %battery state, from 0 to 100.
 */
unsigned char battery_getState(void);

/**
 * @brief Reads the state of the %battery, in [V] units.
 *
 * This function returns the last calculated value for the %battery voltage. 
 * The calculation of the voltage is done in the ISR, each time a reading is complete. 
 * That means, when the interrupts are not enabled, the reading will not be
 * actual neither accurate.
 * 
 * @return The %battery voltage, from 0 to 600 (when using 6V %battery), or to 1200 (when using 12V %battery).
 */
unsigned int battery_getVoltage(void);

/**
 * @brief Read the state of the %battery, wether voltage or percentage.
 *
 * This function is the same as battery_getState() or battery_getVoltage(), and is included for compatibility issues 
 * (each sensor module should have a %_read() function).
 * @see battery_getState battery_getVoltage
 *
 * @param type Defines which value will be returned. Use BATT_READ_VOLT or BATT_READ_PERC to obtain Voltage or Percent, respectively.
 * @return same as in battery_getState() or battery_getVoltage()
 */
unsigned int battery_read(unsigned char type);

/**@}*/
#endif
